export default {
    namespaced:true,
    state:{
        goodList:uni.getStorageSync('goodsList') || []
    },
    getters: {
        getCartList(state){
            return state.goodList
        },
        // 获取总数量
      getTotal (state) {
          let total = 0
          state.goodList.forEach(item => {
            total += item.goods_count
          })
    
          return total
        },
        // 获取选中的总数量
        getSelectTotal(state){
            let total = 0
            state.goodList.forEach(item => {
                    if(item.goods_state){
                  total += item.goods_count
                }
            })
    
          return total
        },
        // 获取总金额
        getSelectAmount(state){
            let amount = 0
            state.goodList.forEach(item => {
            if(item.goods_state){
                  amount += item.goods_count * item.goods_price
                }
            })
            // console.log(amount)
          return amount
        },
        isAllCheck (state) {
            let isAllCheck = true
            state.goodsList.forEach(item => {
              if (!item.goods_state) {
                isAllCheck = false
              }
            })
      
            return isAllCheck
          }
      },
    mutations:{
        // 添加商品
        addToCart(state,newGoodList){
            // 判断是否重复
            const findCart = state.goodList.find((item)=>{
               return item.goods_id===newGoodList.goods_id
            })
            if(findCart){
                // 有重复，直接增加
                findCart.goods_count += 1
            }else{
                state.goodList.push(newGoodList)
            }
            console.log(state.goodList)
            // 保存本地
            uni.setStorageSync('goodsList', state.goodList)
        },
        // 改变选中状态
        changeRadio(state,goods){
           const findRadio = state.goodList.find(item=>
                 item.goods_id===goods.goods_id
           )
            if(findRadio){
                findRadio.goods_state = goods.goods_state
            }
            // 保存到本地
            uni.setStorageSync('goodsList', state.goodList)
        },
        // 改变数量
        changeNumber(state,goods){
            const findRadio = state.goodList.find(item=>
                  item.goods_id===goods.goods_id
            )
             if(findRadio){
                 findRadio.goods_count = goods.goods_count
             }
             // 保存到本地
             uni.setStorageSync('goodsList', state.goodList)
         },
        //  删除商品
        deleGoods(state,goods){
            // const findRadio = state.goodList.findIndex(v=>v.goods_id===goods.goods_id)
            // if(findRadio>=0){
            //     state.goodList.splice(findRadio,1)
            // }
            state.goodList = state.goodList.filter(v=>v.goods_id!==goods.goods_id)
           // 保存到本地
           uni.setStorageSync('goodsList', state.goodList)
        },
        // 全选
        updateAllGoodsState(state,goods_state){
            state.goodList.forEach(v=>{
                v.goods_state=goods_state
            })
            // 保存到本地
           uni.setStorageSync('goodsList', state.goodList)
        }
    }
}